package com.scuvc.controller.user;

import com.scuvc.entity.CarInfo;
import com.scuvc.entity.RecordInfo;
import com.scuvc.entity.UserInfo;
import com.scuvc.utils.ImageUtils;
import com.scuvc.utils.StaticUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.Date;

public class RentCarView extends JFrame {

    public void rentStart(){
        setTitle("租赁业务办理中心");
        //设置窗体起始位置
        setLocation(400,100);
        //设置窗体大小
        setSize(570,520);
        //把图片添加到标签里
        ImageIcon img = new ImageIcon("src/images/bg_addCar.jpg");
        ImageIcon bg = ImageUtils.createAlphaIcon(img,0.5f, getWidth(),getHeight());
        JLabel label = new JLabel(bg);
        //设置标签的大小
        label.setSize(getWidth(),getHeight());
        //把标签放在分层面板的最底层
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //把窗口面板设为内容面板并设为透明，流动布局
        JPanel pan = (JPanel)getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new BorderLayout());
        //设置标题栏
        setTop(pan);

        JPanel panCenter = new JPanel();
        panCenter.setOpaque(false);
        GridLayout grid = new GridLayout(6,4);
        grid.setVgap(20);
        grid.setHgap(10);
        panCenter.setLayout(grid);
        Font font = new Font("仿宋",Font.BOLD,24);

        JLabel j1L1 = new JLabel("车\u0020牌\u0020号：");
        j1L1.setFont(font);
        j1L1.setHorizontalAlignment(SwingConstants.CENTER);
        JTextField j1f2 = new JTextField();
        j1f2.setFont(font);
        JButton j1b3 = new JButton("搜\u3000索");
        j1b3.setFont(font);
        JLabel j1L4 = new JLabel();

        JLabel j2L1 = new JLabel("品\u3000\u3000牌：");
        j2L1.setFont(font);
        j2L1.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel j2L2 = new JLabel();
        j2L2.setFont(font);
        JLabel j2L3 = new JLabel("模\u3000\u3000式：");
        j2L3.setFont(font);
        j2L3.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel j2L4 = new JLabel();
        j2L4.setFont(font);

        JLabel j3L1 = new JLabel("类\u3000\u3000别：");
        j3L1.setFont(font);
        j3L1.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel j3L2 = new JLabel();
        j3L2.setFont(font);
        JLabel j3L3 = new JLabel("排\u3000\u3000量：");
        j3L3.setFont(font);
        j3L3.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel j3L4 = new JLabel();
        j3L4.setFont(font);

        JLabel j4L1 = new JLabel("每日租金：");
        j4L1.setFont(font);
        j4L1.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel j4L2 = new JLabel();
        j4L2.setFont(font);
        JLabel j4L3 = new JLabel("市\u0020场\u0020价：");
        j4L3.setFont(font);
        j4L3.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel j4L4 = new JLabel();
        j4L4.setFont(font);

        JLabel j5L1 = new JLabel("押\u3000\u3000金：");
        j5L1.setFont(font);
        j5L1.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel j5L2 = new JLabel();
        j5L2.setFont(font);
        JLabel j5L3 = new JLabel("租赁状态：");
        j5L3.setFont(font);
        j5L3.setHorizontalAlignment(SwingConstants.CENTER);
        JLabel j5L4 = new JLabel();
        j5L4.setFont(font);

        font = new Font("仿宋",Font.BOLD,20);
        JLabel j6L1 = new JLabel();
        JButton j6b2 = new JButton("租借汽车");
        j6b2.setFont(font);
        JButton j6b3 = new JButton("重\u3000\u3000置");
        j6b3.setFont(font);
        JLabel j6L4 = new JLabel();

        panCenter.add(j1L1);
        panCenter.add(j1f2);
        panCenter.add(j1b3);
        panCenter.add(j1L4);

        panCenter.add(j2L1);
        panCenter.add(j2L2);
        panCenter.add(j2L3);
        panCenter.add(j2L4);

        panCenter.add(j3L1);
        panCenter.add(j3L2);
        panCenter.add(j3L3);
        panCenter.add(j3L4);

        panCenter.add(j4L1);
        panCenter.add(j4L2);
        panCenter.add(j4L3);
        panCenter.add(j4L4);

        panCenter.add(j5L1);
        panCenter.add(j5L2);
        panCenter.add(j5L3);
        panCenter.add(j5L4);

        panCenter.add(j6L1);
        panCenter.add(j6b2);
        panCenter.add(j6b3);
        panCenter.add(j6L4);

        pan.add(panCenter,BorderLayout.CENTER);
        //设置窗体大小是否可调整
        setResizable(false);
        //设置窗体是否可见
        setVisible(true);
        //单击关闭按钮执行的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        j1f2.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                j1f2.setBackground(Color.BLACK);
                j1f2.setForeground(Color.WHITE);
            }
            @Override
            public void focusLost(FocusEvent e) {
                j1f2.setBackground(Color.WHITE);
                j1f2.setForeground(Color.BLACK);
                String carNo = j1f2.getText();
                if(carNo.equals("")){
                    JOptionPane.showMessageDialog(null, "车牌号不能为零",
                            "提示", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        //搜索业务
        j1b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String carNo = j1f2.getText();
                if(!carNo.equals("")){
                    CarInfo carInfo = StaticUtils.carInfoService.getCarInfoByCarNoOne(carNo);
                    if(carInfo!=null){
                        j2L2.setText(carInfo.getBrand().getBrand_name());
                        j2L4.setText(carInfo.getModel().getModel_name());
                        j3L2.setText(carInfo.getCategory().getCategory_name());
                        j3L4.setText(carInfo.getDisplacement());
                        j4L2.setText(""+carInfo.getPrice());
                        j4L4.setText(""+carInfo.getCar_amount());
                        j5L2.setText(""+carInfo.getDeposit());
                        j5L4.setText(carInfo.getStatus()==1?"可租":"已租");
                    }else{
                        JOptionPane.showMessageDialog(null, "车库没有这辆车",
                                "提示", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        });
        //重置业务
        j6b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                j1f2.setText("");
                j2L2.setText("");
                j2L4.setText("");
                j3L2.setText("");
                j3L4.setText("");
                j4L2.setText("");
                j4L4.setText("");
                j5L2.setText("");
                j5L4.setText("");
            }
        });
        //租车业务
        j6b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                    // TODO
                    if (row == 1) {
                        //租车成功后数据更新
                        StaticUtils.carList= StaticUtils.carInfoService.getAllPage
                                (StaticUtils.pageBean.getCurPage(),
                                StaticUtils.pageBean.getPageSize());
                        UserView uv = new UserView();
                        setVisible(false);
                        uv.start();
                    } else {
                        RentCarView rcv = new RentCarView();
                        setVisible(false);
                        rcv.rentStart();
                    }
                }
            }
        });
    }
    private void setTop(JPanel pan) {
        JPanel panTop = new JPanel();
        panTop.setLayout(new GridLayout(1,1));
        panTop.setOpaque(false);
        JLabel jTop = new JLabel("租车办理");
        jTop.setPreferredSize(new Dimension(
                jTop.getPreferredSize().width,100));
        jTop.setFont(new Font("仿宋",Font.BOLD,36));
        jTop.setHorizontalAlignment(SwingConstants.CENTER);
        panTop.add(jTop);
        pan.add(panTop,BorderLayout.NORTH);
    }
}

